package com.hepinghospital.user.ui.activity.consult;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.activity.home.BookDoctorActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.Doctor;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import jz.joyoung.robot.util.DpUtil;

/**
 * Created by HP on 2020/2/26.
 */

@Deprecated
public class CloudDepartmentActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitile;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;

    MyAdapter departmentAdapter;

    List<Doctor> departmentList = new ArrayList<>();

    @Override
    protected void initViews() {
        tvTitile.setText("找医生");
        LinearLayoutManager manager = new LinearLayoutManager(CloudDepartmentActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        departmentAdapter = new MyAdapter();
        recyclerView.setAdapter(departmentAdapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(CloudDepartmentActivity.this, 1);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });

        edt_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {   // 按下完成按钮，这里和上面imeOptions对应
                getDepartmentList();
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        getDepartmentList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_cloud_department;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(CloudDepartmentActivity.this).inflate(R.layout.item_department_online, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString("deptId", departmentList.get(position).id);
                startActivity(BookDoctorActivity.class, bundle);
            });
            holder.tv_department.setText(departmentList.get(position).name);
            holder.tv_info.setText(departmentList.get(position).introduction);
        }

        @Override
        public int getItemCount() {
            return departmentList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_department, tv_info;

            public Holder(View itemView) {
                super(itemView);
                tv_department = itemView.findViewById(R.id.tv_department);
                tv_info = itemView.findViewById(R.id.tv_info);
            }
        }
    }


    private void getDepartmentList() {
        RestProxy.getInstance().getDepartmentList(edt_search.getText().toString(), new BaseObserver<List<Doctor>>() {
            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(CloudDepartmentActivity.this, e);

            }

            @Override
            public void onSuccess(List<Doctor> doctors) {
                departmentList.clear();
                departmentList.addAll(doctors);
                departmentAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
